package com.pudu.mydemo.act;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.pudu.mydemo.key.KeyboardUtil;
import com.pudu.mydemo.utils.ToastUtils;
import com.pudutech.pdrobot.R;

import java.lang.reflect.Method;

/**
 * Created by guiliu on 2016/7/26.
 */
public class ThroughActivity extends BaseHomeActivity implements View.OnClickListener{
    private ImageView desk,ok,cancel;
    private EditText desk_number;
    private KeyboardUtil keyboardUtil;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Method setShowSoftInputOnFocus = null;
        try {
            setShowSoftInputOnFocus = desk_number.getClass().getMethod(
                    "setShowSoftInputOnFocus", boolean.class);
            setShowSoftInputOnFocus.setAccessible(true);
            setShowSoftInputOnFocus.invoke(desk_number, false);
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch(Exception e){
            e.printStackTrace();
        }

    }

    @Override
    protected int getLayoutId() {
        return R.layout.through_mode;
    }

    @Override
    public void initView() {
        super.initView();
        cancel = (ImageView)findViewById(R.id.cancel);
        ok=(ImageView)findViewById(R.id.ok);
        desk = (ImageView)findViewById(R.id.desk);
        desk_number=(EditText) findViewById(R.id.desk_number);
        keyboardUtil = new KeyboardUtil(ThroughActivity.this, ThroughActivity.this, desk_number);
        keyboardUtil.hideKeyboard();
    }

    @Override
    public void initEvent() {
        super.initEvent();
        cancel.setOnClickListener(this);
        ok.setOnClickListener(this);
//        desk.setOnClickListener(this);

        desk_number.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
               keyboardUtil.showKeyboard();
                return false;
            }
        });


        desk_number.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public void onClick(View v) {
      switch (v.getId()) {
          case R.id.cancel:
              finish();
              break;
          case R.id.ok:
              launchActivity();
              break;
        /*  case R.id.desk:
              launchActivityForResult();
              break;*/
      }

    }

    public void launchActivity(){
        Intent intent = new Intent();
        intent.setAction("Intent_Action_Move");
        intent.putExtra("action","ACTION_THROUGH_MOVE");
        Log.e("through","desknumber:"+desk_number.getText().toString());
        if(desk_number.getText().toString().equals("")) {
            Toast.makeText(this,"请输入桌号",Toast.LENGTH_LONG).show();
        } else {
            intent.putExtra("data",desk_number.getText().toString());
            startActivity(intent);
            finish();
        }

    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if(ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if(isShouldHideInput(v,ev)) {
                keyboardUtil.hideKeyboard();
            }
        }
        return super.dispatchTouchEvent(ev);

    }

    private boolean isShouldHideInput(View v, MotionEvent event) {
        int[] location = keyboardUtil.getKeyboardViewLocation();
        if(v != null && (v instanceof EditText)) {
            int[] l = {0, 0};
            v.getLocationInWindow(l);
            int left = l[0],top = l[1];
            int right = left+v.getWidth();
            int bottom = top + v.getHeight();
            if(event.getX() > left && event.getX() < right && event.getY() > top && event.getY() < bottom) {
                return false;
           } else if(event.getX() > location[0] && event.getX() < location[1] && event.getY() > location[2] && event.getY() < location[3]) {
                return false;
            }else {
                return true;
            }
        }
        return false;
    }



}
